package org.wzq.utils;

/**
 * 自定义日志 有开关
 *
 * @author llbt_wzq
 */
public class Logger {
    private static Logger defaultLogger = new Logger();
    private static String DEFAULT_TAG = "android mdm";

    public static Logger getLogger() {
        return defaultLogger;
    }

    private Logger() {
        this.TAG = DEFAULT_TAG;
        this.IS_SHOW_LOG = true;
    }

    public static Logger newLogger(boolean show, Class<?> clazz) {
        return new Logger(show, clazz.getSimpleName());
    }

    public static Logger newLogger(boolean show, Object obj) {
        return new Logger(show, obj.getClass().getSimpleName());
    }

//    public static Logger newLogger(boolean show, String tag) {
//        return new Logger(show, tag);
//    }

    private Logger(boolean show, String tag) {
        this.TAG = tag;
        this.IS_SHOW_LOG = show;
    }


    /** 日志标签 tag */
    private String TAG = "AppClient";

    private boolean IS_SHOW_LOG = true;

    /** 单条log的长度上线 */
    private static final int LOG_MAX_LEN = 3000;

    public void v(String msg) {
        if (!IS_SHOW_LOG) {
            return;
        }
        showLog(msg, LOGV);
    }

    public void d(String msg) {
        if (!IS_SHOW_LOG) {
            return;
        }
        showLog(msg, LOGD);
    }

    public void i(String msg) {
        if (!IS_SHOW_LOG) {
            return;
        }
        showLog(msg, LOGI);
    }

    public void w(String msg) {
        if (!IS_SHOW_LOG) {
            return;
        }
        showLog(msg, LOGW);
    }

    public void e(String msg) {
        if (!IS_SHOW_LOG) {
            return;
        }

        showLog(msg, LOGE);

        // 打印出错地点
        String errorPoint = "error at " + Thread.currentThread().getStackTrace()[2].getMethodName() + //
                "class:" + Thread.currentThread().getStackTrace()[3].getClassName() + //
                "::" + Thread.currentThread().getStackTrace()[3].getMethodName();

        android.util.Log.e(TAG, errorPoint);
    }

    /**
     * 打印日志且将日志输出到sd卡中
     *
     * @param msg
     */
    public void log2Sd(String msg) {

    }

    /**
     * 在方法内部调用，打印调用信息
     */
    public void whoInvokeMe() {
        if (!IS_SHOW_LOG) {
            return;
        }

        String callerInfo = Thread.currentThread().getStackTrace()[3].getMethodName() + //
                " called by " + Thread.currentThread().getStackTrace()[4].getClassName() + //
                "::" + Thread.currentThread().getStackTrace()[4].getMethodName();

        android.util.Log.i(TAG, callerInfo);
    }

    /**
     * 打印流程,默认d levle
     *
     * @param msg 可以为null
     */
    public void printProcess(String msg) {
        if (!IS_SHOW_LOG) {
            return;
        }

        String info = Thread.currentThread().getStackTrace()[3].getClassName() + //
                "::" + Thread.currentThread().getStackTrace()[3].getMethodName();
        if (msg != null) {
            info += "  " + msg;
        }

        d(info);
    }

    /**
     * 打印流程,verbose levle
     *
     * @param msg 可以为null
     */
    public void printProcessV(String msg) {
        if (!IS_SHOW_LOG) {
            return;
        }

        String info = Thread.currentThread().getStackTrace()[3].getClassName() + //
                "::" + Thread.currentThread().getStackTrace()[3].getMethodName();
        if (msg != null) {
            info += "  " + msg;
        }

        v(info);
    }

    /**
     * 打印流程 静态方法中调用会不准
     *
     * @param msg string-msg
     */
    public void printError(String msg) {
        if (!IS_SHOW_LOG) {
            return;
        }

        String info = Thread.currentThread().getStackTrace()[3].getClassName() + //
                "::" + Thread.currentThread().getStackTrace()[3].getMethodName();
        if (msg != null) {
            info += "  " + msg;
        }

        e(info);
    }

    /**
     * 根据等级，打印日志，保持日志
     *
     * @param msg
     * @param level
     */
    private void showLog(String msg, int level) {
        // 过长msg的处理
        while (msg.length() >= LOG_MAX_LEN) {
            // 首先打印前LOG_LENGTH个字符
            String str1 = msg.substring(0, LOG_MAX_LEN);
            showLogShort(str1, level);
            msg = msg.substring(LOG_MAX_LEN);
        }

        if (msg.length() != 0) {
            showLogShort(msg, level);
        }
    }

    private void showLogShort(String str, int level) {
        switch (level) {
            case LOGV:
                android.util.Log.v(TAG, str);
                break;
            case LOGD:
                android.util.Log.d(TAG, str);
                break;
            case LOGI:
                android.util.Log.i(TAG, str);
                break;
            case LOGW:
                android.util.Log.w(TAG, str);
                break;
            case LOGE:
                android.util.Log.e(TAG, str);
                break;
        }
    }

    /** 保存的日志等级 */
    private final int LOGV = 0;
    private final int LOGD = 1;
    private final int LOGI = 2;
    private final int LOGW = 3;
    private final int LOGE = 4;
}
